package zoo.product.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import zoo.product.dao.ProductDao;
import zoo.product.domain.ProductCommand;
import zoo.reviewboard.user.dao.ProductReviewBoardDao;
import zoo.reviewboard.user.domain.ProductReviewBoardCommand;
import zoo.util.PagingUtil;
import zoo.util.StringUtil;

@Controller
public class ProductDetailController {
	
	private int pageSize = 10;
	private int blockCount = 10;
	
	@Autowired
	private ProductReviewBoardDao reviewDao;
	
	@Autowired
	private ProductDao productDao;
	
	@ModelAttribute("pro")
	public ProductCommand formBacking(){
		return new ProductCommand();
	}
		
	@ModelAttribute("p_option")
	protected List<String> options(@RequestParam("p_code")int p_code) throws Exception{
		String[] op  = productDao.selectOption(p_code).split("-");
		List<String> options =new ArrayList<String>();
		if(op != null && op.length>0){
			for(int i=0;i<op.length;i++){
			
				if(i==0){options.add(op[0]);
				}else if(i==1){options.add(op[1]);
				}else if(i==2){options.add(op[2]);
				}else if(i==3){options.add(op[3]);
				}else if(i==4){options.add(op[4]);
				}
			}
		}
		return options;
	}
	
				
	@RequestMapping("/main/detailPro.do")
	public ModelAndView process(@RequestParam("p_code")int p_code,
			@RequestParam(value="sc", defaultValue="3")String sc,
			@RequestParam(value="curentPage", defaultValue="1")int currentPage,
			@RequestParam(value="keyField", defaultValue="0")String keyField,
			@RequestParam(value="keyWord", defaultValue="0")String keyWord){
		
		ProductCommand board = productDao.selectProMemberBoard(p_code);
		
		//히트
		productDao.updateProHit(p_code);
		
		//줄바꿈 처리
		board.setP_content(StringUtil.parseBr(board.getP_content()));
		
		if(sc == "1"){
			ModelAndView mav = new ModelAndView();
			mav.setViewName("detail_product");
			mav.addObject("sc",sc);
			
		}
		
		String pagingHtml;
		
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("keyField", keyField);
		map.put("keyWord", keyWord);
		map.put("p_code",p_code);
		
		//총 글의 갯수 또는 검색된 글의 갯수
		int count = reviewDao.selectReviewCount(map);
		
		PagingUtil page = new PagingUtil(keyField, keyWord, currentPage, count, blockCount, pageSize);
		pagingHtml = page.getPagingHtml().toString();
		
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		
		List<ProductReviewBoardCommand> list = null;
		if(count > 0){
			list = reviewDao.selectReviewList(map);
		}else{
			list = Collections.emptyList();
		}
		
		//글 목록에 표시할 연번
		int number = count - (currentPage-1)*pageSize;			
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("detail_product");
		mav.addObject("count", count);
		mav.addObject("list", list);
		mav.addObject("pagingHtml", pagingHtml);
		mav.addObject("number", number);
		mav.addObject("board",board);
		
		return mav;
	}
	
}
